#include "HUDManager.h"
#include "HUDObject.h"
#include "RenderManager.h"
#include "DebugLog.h"

HUDManager::HUDManager(void)
{
}


HUDManager::~HUDManager(void)
{
}

void HUDManager::update()
{
	if (!_hud.empty())
	{
		for (auto it = _hud.begin(); it != _hud.end(); it++)
		{
			(*it)->update();
		}
	}
}

void HUDManager::draw(RenderManager* rm)
{
	if (!_hud.empty())
	{
		for (auto it = _hud.begin(); it != _hud.end(); it++)
		{
			if ((*it)->isVisible())
				(*it)->draw(rm);
		}
	}
}

void HUDManager::addObject(HUDObject* ob)
{
	HUDObject* object = doesHUDObjectExist(ob);
	if (object == nullptr)
	{
		_hud.push_back(ob);
	}
	else
	{
		Debug::log("HUDManager::addObject - WARNING: trying to create new HUD object with already existing ID. Object deleted");
		delete ob;	// prevent creation of HUDObject with identical IDs
	}
}

HUDObject* HUDManager::doesHUDObjectExist(HUDObject* ob)
{
	for (auto it = _hud.begin(); it != _hud.end(); it++)
	{
		if (ob->getID() == (*it)->getID())
			return (*it);
	}
	return nullptr;
}

HUDObject* HUDManager::getHUDObject(const std::string& id)
{
	for (auto it = _hud.begin(); it != _hud.end(); it++)
	{
		if (id == (*it)->getID())
			return (*it);
	}
	return nullptr;
}

void HUDManager::removeObject(const std::string& id)
{
	for (auto it = _hud.begin(); it != _hud.end(); it++)
	{
		if (id == (*it)->getID())
		{
			_hud.erase(it);
			break;
		}
	}
}